
var reback = angular.module("reback", ["angular-popups", "scService", "ui.router", "loginApp"]);

reback.controller("rebackController", function($scope, $rootScope, $q, $window, scCodeService, $location,pubFun) {      
    //获取openId
    // alert("reback")
    $scope.getOpenId = function() {
        var deferred = $q.defer();
        var postData = {
            "source": mainConfig.source,
            "merchantCode": mainConfig.merchantCode,
            "code": $scope.paramsData.code
        };
        scCodeService.getOpenId(postData).then(function(data) {           
            if (data.data.openid) {
                var openId = data.data.openid;
                 //alert("openID数据"+openId);
                var access_token = data.data.access_token;
                localStorage.setItem("openId", openId);
                localStorage.setItem("access_token", access_token);
                deferred.resolve($scope.paramsData.state);
            } else {
                var state = $scope.paramsData;
                deferred.reject(state);
            }
        });
        return deferred.promise;
    };
    //获取链接上code和state参数
    $scope.GetRequest = function() {
            var deferred = $q.defer();
            var url = location.search; //获取url中"?"符后的字串 
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            $scope.paramsData = theRequest;
            //alert(JSON.stringify($scope.paramsData));
            deferred.resolve();
            return deferred.promise;

        }
    // 进入reback执行的方法
    $scope.GetRequest().then(function() {
        return $scope.getOpenId();
    }).then(function(data) {
        //alert("openId获取成功")
        //根据state的值跳转不同界面
        var deferred = $q.defer();
        if (data == 2) { 
            //state为2则登录后跳转至扫码页面
            return pubFun.autoLogin();
        } else if(data==1) { //state为1跳转至首页
            deferred.reject();
            window.location.href = mainConfig.hostUrl + "index.html?#/index_";
        }
        return deferred.promise;
    }, function(data) {
        window.location.href = mainConfig.hostUrl + "enter.html?state=" + data
    }).then(function() {
        pubFun.getUserCard();
        return $q.all([pubFun.getPending(), pubFun.getOngoingOperation()])
    },function(data){
        if(data == "201" || data == "0101001"){
            $rootScope.showLogin = true;            
        }else{
            $rootScope.showLogin = false;
        }        
    }).then(function(data) {
        if (globalFun.isEmptyObj(data[0]) == false) {
            $scope.notPayCardNo = data[0][0].orderNo;
            window.location.href = mainConfig.hostUrl + "index.html?#/orderPay/"+ $scope.notPayCardNo;
        }else if(globalFun.isEmptyObj(data[1].chargeList) == false){
            var qrCode = data[1].chargeList[0].chargerDetail.qrCode,
                    startTime = data[1].chargeList[0].startTime,
                    startTimestr  = globalFun.timeToNrm(startTime),
                    chargeSn=data[1].chargeList[0].chargeSn;
                pubFun.goToCharging(qrCode,chargeSn,startTime,startTimestr);
        }else if(globalFun.isEmptyObj(data[1].reservationList) == false){
            var qrCode = data[1].reservationList[0].chargerDetail.qrCode,
                reservationSn = data[1].reservationList[0].reservationSn;
                pubFun.goToAppointing(qrCode,reservationSn);
        } else {
            window.location.href = mainConfig.hostUrl + "scanCode.html";
        }
    });
});





